<?php

/**
 * 自动生成 MerchantService
 * 
 * Apps/Common/Service/MerchantService.class.php
 * admin @ 2015-06-11
 */

namespace Common\Service;

use Common\Service\SysDictionaryService;

class MerchantService {

  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerchantService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'name' => '',
      'telphone' => '',
      'contact' => '',
      'phone' => '',
      'email' => '',
      'address' => '',
      'status' => 26 ,
      'type' => 19 ,
      'qualified' => '',
      'qualified_code' => '',
      'area' => '',
    );
  }
  
  function get_by_id( $id ) {
    $Merchant = M('Merchant');
    $data = $Merchant->find($id);
    return $data ? $data : array();
  }
  
  function get_by_manager_user_id( $user_id ) {
    $Merchant = M('Merchant');
    $data = $Merchant
      ->field('m.*')
      ->alias('m')
      ->join('mer_user_manager mum on mum.mer_id = m.id')
      ->where('mum.sys_user_id = %d' , $user_id )
      ->find();
    return $data ? $data : array();
  }
    
  function get_status() {
    $sys_dic = SysDictionaryService::instance();
    return $sys_dic->get_merchant_status() ;
  }
  
  function get_type() {
    $sys_dic = SysDictionaryService::instance();
    return $sys_dic->get_merchant_type() ;
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'keyword' => '',
      'type' => '',
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $Merchant = M('Merchant');

    $where = array();

    if (!empty($config['keyword'])) {
      $k = $config['keyword'] ;
      $where['_string'] = " name like '%$k%' or contact like '%$k%' or phone = '$k' or telphone = '$k' ";
    }
    
    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }
    
    if (!empty($config['type'])) {
      $where['type'] = $config['type'];
    }

    if ($config['count']) {
      return $Merchant->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $Merchant->where($where)->limit($limit)->order($order)->select();
      //echo $Merchant->_sql();
      return $data ? $data : array();
    }
  }

  function create($data) {
    $Merchant = M('Merchant');
    $ret = $Merchant->add($data);
    if ($ret) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function update($id, $data) {
    $Merchant = M('Merchant');

    $ret = $Merchant->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $Merchant = M('Merchant');
    $ret = $Merchant->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
